package com.qingcheng.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;


/**
 * @Author: wy
 * @Date:2019/9/11 17:21
 */
@Controller
@ResponseBody
@RequestMapping("/upload")
public class FileController {
             @Autowired
              private HttpServletRequest request;
        @RequestMapping("/native")
     public String  nativeUpload(@RequestParam("file")MultipartFile file){
              //拿到图片文件要存储的真实路径
            String img = request.getSession().getServletContext().getRealPath("img");
                   File  realpath=new File(img);
                    if(!realpath.exists()){
                        realpath.mkdir();
                    }
               File destFile=new File(realpath,file.getOriginalFilename());
                    //读写流
            try {
                file.transferTo(destFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return  "http://localhost:9101/img/"+file.getOriginalFilename();

        }
}
